//
//  KYTabBarController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/22.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYTabBarController.h"
#import "KYTabBar.h"
#import "KYNavigationController.h"
#import "JSTHomeWebViewController.h"
#import "JSTIMListViewController.h"
#import "JSTInterestcircleViewController.h"
#import "JSTPersonalViewController.h"

@interface KYTabBarController ()<KYTabBarDelegate>
@property (nonatomic, weak) KYTabBar *customTabBar;

@end

@implementation KYTabBarController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 1.初始化tabbar
    [self setUpTabBar];
    
    // 2.初始化子控制器
    [self setUpChildViewControllers];
}

/**
 *  每次即将显示的时候必须移除系统自带的
 */
 - (void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];
     if (IOS_SYSTEM_VERSION < 11) {
         for (UIControl *tabBarBtn in self.tabBar.subviews) {
             if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                 [tabBarBtn removeFromSuperview];
             }
         }
     }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 移除之前的4个UITabBarButton
    if (IOS_SYSTEM_VERSION >= 11) {
        for (UIControl *tabBarBtn in self.tabBar.subviews) {
            if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [tabBarBtn removeFromSuperview];
            }
        }
    }
}

/**
 *  初始化tabbar
 */
- (void)setUpTabBar
{
    // 0.设置tabbar的背景
    self.tabBar.backgroundImage = [UIImage imageWithName:@"tabbar_background"];
    self.tabBar.shadowImage = [[UIImage alloc] init];
    
     // 1.自定义的tabbar 覆盖系统的就行
    KYTabBar *customTabBar = [[KYTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;  // frame
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

/**
 *  初始化子控制器
 */
- (void)setUpChildViewControllers
{
    // 1.主页
    JSTHomeWebViewController *home = [[JSTHomeWebViewController alloc] init];
    [self setUpChildViewController:home title:NSLocalizedString(@"主页", nil) imageName:@"tab_bar_icon_home_nam" selectedImageName:@"tab_bar_icon_home_active"];
    
    // 2.会话
    JSTIMListViewController *session = [[JSTIMListViewController alloc] init];
    [self setUpChildViewController:session title:NSLocalizedString(@"会话", nil) imageName:@"tab_bar_icon_address_nam" selectedImageName:@"tab_bar_icon_address_active"];
    
    // 3.动态
    JSTInterestcircleViewController *status = [[JSTInterestcircleViewController alloc] init];
    [self setUpChildViewController:status title:NSLocalizedString(@"动态", nil) imageName:@"tab_bar_icon_Interest_nam" selectedImageName:@"tab_bar_icon_Interest_active"];
    
    // 4.个人中心
//    JSTPersonalViewController *mine = [[JSTPersonalViewController alloc] init];
//    [self setUpChildViewController:mine title:NSLocalizedString(@"个人中心", nil) imageName:@"tab_bar_icon_personal_nam" selectedImageName:@"tab_bar_icon_personal_active"];
}

/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器
 *  @param title             标题
 *  @param imageName         图片
 *  @param selectedImageName 选中的图片
 */
- (void)setUpChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置数据
    childVc.tabBarItem.image = [UIImage imageWithName:imageName];
    childVc.tabBarItem.selectedImage = [UIImage oringinalImageWithName:selectedImageName];
    childVc.title = title;
    
    // 2.包装
    KYNavigationController *nav = [[KYNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    // 3.添加按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}

/**
 *  tabBar选中了按钮
 *
 *  @param from   以前的位置
 *  @param to     现在的位置
 */
- (void)tabBar:(KYTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
    self.selectedIndex = to;
}
@end
